/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd.            */
/*                                                                 */
/*  NAME      = t_sqlserver_parse_prelogin.c                       */
/*  NOTE      =                                                    */
/*  DATE      = 2015/02/26 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <string.h>
#include <CUnit/Basic.h>

#include <frcomm.h>
#include <sqlserver/config.h>
#include <sqlserver/sqlserver_core.h>
#include <sqlserver/sqlserver_common.h>

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}

static void __case1(void)
{
    Conntrack *p_st_conn = NULL;
    L7session_sqlserver_private *p_st_private = NULL;
    Frhash_header *p_st_hash_report = NULL;
    int i_whole_flg = FR_NO;
    int i_ser_cli_flg = MSSQL_CLI2SRV;
    int i_ret = 0;

    i_ret = sqlserver_parse_prelogin(p_st_conn, p_st_private, p_st_hash_report, i_whole_flg, i_ser_cli_flg);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_TODO);
    return;

}

static void __case2(void)
{
    Conntrack *p_st_conn = NULL;
    L7session_sqlserver_private *p_st_private = NULL;
    Frhash_header *p_st_hash_report = NULL;
    int i_whole_flg = FR_YES;
    int i_ser_cli_flg = MSSQL_CLI2SRV;
    int i_ret = 0;
    L7_fragment_sqlserver *p_st_fragment = NULL;
    Frbyte_Packet *p_st_data = NULL;

    p_st_private = (L7session_sqlserver_private *) frmalloc(sizeof(L7session_sqlserver_private));
    memset(p_st_private, 0x00, sizeof(L7session_sqlserver_private));

    p_st_fragment = (L7_fragment_sqlserver *) frmalloc(sizeof(L7_fragment_sqlserver));
    memset(p_st_fragment, 0x00, sizeof(L7_fragment_sqlserver));

    p_st_data = bytebuff_creat();
    bytebuff_add(p_st_data, (char *) "\x00\x01\x00\x01\x00", 5);

    p_st_fragment->p_st_data = p_st_data;
    p_st_private->p_st_fragment = p_st_fragment;

    i_ret = sqlserver_parse_prelogin(p_st_conn, p_st_private, p_st_hash_report, i_whole_flg, i_ser_cli_flg);

    CU_ASSERT_EQUAL(i_ret, APM_L7_ANALYZE_SUCC);

    bytebuff_free(&p_st_data);
    frfree(p_st_fragment);
    sqlserver_destroy_request(&(p_st_private->p_st_req));
    frfree(p_st_private);
    return;
}

int add_suite_sqlserver_parse_prelogin(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" sqlserver_parse_prelogin()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    //增加测试用例
    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
